<?php


declare(strict_types=1);

namespace App\Controller\Abell;

use App\Controller\AbstractController;
use App\Server\CurlApi;
use App\Utils\ApiUtils;
use App\Utils\Upload;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Extend\Wx\Wxlogin as WxloginExtend;

/**
 * @Controller()
 */
class WxmsgController extends AbstractController
{
    /** POST https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
     * 把媒体文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息
     * @param FormData $media form-data 中媒体文件标识，有filename、filelength、content-type等信息
     * @return array
     * Abell 2020/5/14
     * @RequestMapping(path="/abell/wxmsg/uploadimg", methods="post,get")
     */
    public function uploadimg(RequestInterface $request){
        var_dump('请求时间');
        var_dump(date('Y-m-d H:i:s',time()));
        $data = make(WxloginExtend::class)->check_token_redis(false);
        $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$data.'&type=image';
        var_dump($url);
        $temp_img_path = $request->file('img');
        $result = $this->upload_media_test($temp_img_path,$data);
        return $this->response->json(ApiUtils::send(0,'ok',$result,1));
    }

    public function upload_media_test($file,$access_token){
        $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image";
        $file_data['media']=new CURLFile(realpath($file));
        $result=json_decode(uploadMedia($url,$file_data),1);
        var_dump($result);
        return $result;
    }

    private function uploadMedia($url,$fields){
        $ch = curl_init($url) ;
        curl_setopt ($ch, CURLOPT_SAFE_UPLOAD, false);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch) ;
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
        curl_close($ch);
        return $result;
    }

    private function curlFilePost($url,$post){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_VERBOSE,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible;)");
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;

    }

    /** POST https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
     * 把媒体文件上传到微信服务器。目前仅支持图片。用于发送客服消息或被动回复用户消息
     * @param FormData $media form-data 中媒体文件标识，有filename、filelength、content-type等信息
     * @return array
     * Abell 2020/5/14
     */
    public function uploadTempMedia(RequestInterface $request){
        $data = make(WxloginExtend::class)->check_token_redis(false);
        $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.$data.'&type=image';
        $path = BASE_PATH.config('web.img_path').'20200507/5eb37564b6f47.jpg';  // 实际图片路径
        var_dump(fopen($path,'r'));
        $client = new  \GuzzleHttp\Client();
        $data = $client->request('post',$url,['multipart' => [[
                'name'     => 'media',
                'contents' => fopen($path, 'r'),
            ],]])->getBody()->getContents();
        var_dump($data);
        return 'hello';
    }
}
